package com.hbnu.handler;

import com.hbnu.message.Response;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.io.File;
import java.util.Collection;

public class FileDeleteHandler extends SimpleChannelInboundHandler<Collection<String>> {
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, Collection<String> paths) throws Exception {
        int n=0;
        for (String path : paths) {
            File file=new File(path);
            if(!file.exists()) continue;
            if(file.delete()){
                n++;
            }
        }
        System.out.printf("删除了%d个文件\n",n);
        Response response;
        if(n==paths.size()) {
            response = new Response(true, "删除成功！");
        }else {
            response= new Response(true, "删除"+n+"个文件");
        }
        channelHandlerContext.writeAndFlush(response);
    }
}
